package timing.ukulele.item.provider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.ResultCode;
import timing.ukulele.item.client.ProjectClient;
import timing.ukulele.item.persistent.Item;
import timing.ukulele.item.service.ItemService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/provider")
@Api("办理事项接口")
public class ItemProviderController {

    @Resource
    private ProjectClient projectClient;

    private final ItemService itemService;

    public ItemProviderController(ItemService itemService) {
        this.itemService = itemService;
    }


    @GetMapping("/code/list")
    @ApiOperation("根据systemId获取事项编码列表")
    public ResponseResult<List<String>> codeList(@ApiParam(value = "系统id") @RequestHeader(Constant.SYSTEM) String systemId) {
        List<Item> list = itemService.lambdaQuery().eq(Item::getSystemId, systemId).list();
        List<String> codeList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(e -> codeList.add(e.getCode()));
        }
        return ResponseResult.success(codeList);
    }

    @GetMapping("/page")
    @ApiOperation("分页获取事项列表")
    public ResponseResult<Page<Item>> page(
        @ApiParam(value = "查询名称", required = true) @RequestParam(required = false) String name,
        @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
        @ApiParam(value = "系统id") @RequestHeader(Constant.SYSTEM) String systemId) {
        return ResponseResult.success((Page<Item>)itemService.getPage(name, systemId, pageNum, pageSize, new HashMap<>()));
    }

    @PostMapping("/create")
    @ApiOperation("保存事项")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> create(
        @ApiParam(value = "事项对象", required = true) @RequestBody Item item,
        @ApiParam(value = "用户姓名", example = "张三") String username) {
        item.setCreateBy(username);
        itemService.save(item);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改事项信息")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> update(@ApiParam("用户对象") @RequestBody Item item,
        @RequestParam String username) {
        item.setCreateBy(username);
        itemService.updateById(item);
        return ResponseResult.success();
    }

    @PostMapping("/validateItemCode")
    @ApiOperation("校验事项编码是否已存在")
    public ResponseResult<Boolean> validateItemCode(
        @ApiParam("事项编码") @RequestParam String itemCode,
        @ApiParam(value = "更新事项的ID", example = "0") @RequestParam Long updateItemId) {
        Item item = itemService.getByCode(itemCode);
        if (item != null) {
            if (item.getId().equals(updateItemId)) {
                return ResponseResult.success(true);
            }
            return ResponseResult.success(false);
        }
        return ResponseResult.success(true);
    }

    @PostMapping("/saveItem")
    @ApiOperation("保存事项")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> saveItem(
        @ApiParam(value = "事项对象", required = true) @RequestBody Item item,
        @RequestParam String username) {
        item.setCreateBy(username);
        itemService.saveItem(item);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除事项")
    public ResponseResult<String> removeItem(@RequestParam("itemId") Long itemId) {
        ResponseResult<Integer> projectExist = projectClient.checkItemProjectExist(itemId);
        if (projectExist == null) {
            return ResponseResult.error("查询是否该事项是否存在办件失败");
        }
        if (projectExist.getCode() == ResultCode.RPC_ERROR.getCode()) {
            return ResponseResult.error("查询是否该事项是否存在办件失败");
        }
        if (projectExist.getData() == null || projectExist.getData() == 0) {
            itemService.removeById(itemId);
            return ResponseResult.success();
        } else {
            return ResponseResult.error("该事项已产生办件，不能删除");
        }
    }

}
